/*
 * Author: ahxgw
 * Created Time:  Thu 14 Apr 2011 10:59:46 PM PDT
 * Description: 
 */
#include "coms.h"
#include "utils.h"
#include "threadPool.h"

void *threadFunc(void *arg)
{
	int i, n;
	pipeNode *p;
	Node *up, *up2;
	userTable A, B, C;
	char pwd[LEN];
	char sql[BUF_SIZE];
	MYSQL_RES *res, *res2;
	MYSQL_ROW row, row2;

	while (1)
	{
		n = read(pfd1[0], &p, 4);

        if (p->msgh.cmd == REGISTER)
		{
            puts("REGISTER");
			sscanf(p->content, "%s%s", A.nickname, pwd);
			sprintf(sql, "insert into cus_table values('%d', '%s', '%s')", UID, A.nickname, pwd);
			puts(sql);
			mysql_query(&mysql, sql);
			up = new Node(p->fd);
			up->msgh.cmd = REGISTER_ACK;
            up->msgh.error = 0;
            sprintf(up->content, "%d", UID++);
		}
		else if (p->msgh.cmd == LOGIN)
		{
            puts("LOGIN");
			sscanf(p->content, "%s%s%s", A.UID, pwd, A.uIP);
			sprintf(sql, "select nickname, password from cus_table where UID = '%s'", A.UID);
            puts(sql);
			mysql_query(&mysql, sql);
			res = mysql_use_result(&mysql);
			row = mysql_fetch_row(res);
			up = new Node(p->fd);
			up->msgh.cmd = LOGIN_ACK;
			if (row == NULL || strcmp(row[1], pwd))
            {
                up->msgh.error = 1;
				strcpy(up->content, "LOGIN fail!");
            }
            else
			{
                up->msgh.error = 0;
				sprintf(up->content, "%s %s %s", A.UID, row[0], A.uIP);
			}
            mysql_free_result(res);
		}
        else if (p->msgh.cmd == FRIEND_LIST)
        {
            puts("get friend list");
            sscanf(p->content, "%s", A.UID);
            sprintf(sql, "select fUID from friend_list where UID = %s", A.UID);
            puts(sql);
            mysql_query(&mysql, sql);
            res = mysql_store_result(&mysql);
            up = new Node(p->fd);
            up->msgh.cmd = FRIEND_LIST_ACK;
            strcpy(up->content, "friend list:\n");
            while ( (row = mysql_fetch_row(res)) != NULL)
            {
                sprintf(sql, "select nickname from cus_table where UID = '%s'", row[0]);
                mysql_query(&mysql, sql);
                res2 = mysql_store_result(&mysql);
                row2 = mysql_fetch_row(res2);
                sprintf(sql, "%s %s\n", row[0], row2[0]);
                puts(sql);
                strcat(up->content, sql);
                mysql_free_result(res2);
            }
            mysql_free_result(res);
        }
        else if (p->msgh.cmd == FRIEND_CTL)
        {
            puts("friend control");
            sscanf(p->content, "%s%s", A.UID, B.UID);
            up = new Node(p->fd);
            up->msgh.cmd = FRIEND_CTL_ACK;
            n = p->msgh.error;
            if (n == 0)
            {
                sprintf(sql, "select nickname from cus_table where UID = '%s'", B.UID);
                mysql_query(&mysql, sql);
                res = mysql_store_result(&mysql);
                row = mysql_fetch_row(res);
                if (row == NULL)
                    sprintf(up->content, "UID %s is bu cun zai!", B.UID);
                else
                    sprintf(up->content, "%s %s", B.UID, row[0]);
                mysql_free_result(res);
            }
            else if (n == 1)
            {
                sprintf(sql, "insert into friend_list values('%s', '%s')", A.UID, B.UID);
                mysql_query(&mysql, sql);
                sprintf(up->content, "%s is your friend!", B.UID);
            }
            else if (n == 2)
            {
                sprintf(sql, "delete from friend_list where UID = '%s' and fUID = '%s'", A.UID, B.UID);
                mysql_query(&mysql, sql);
                sprintf(up->content, "%s is not your friend!", B.UID);
            }
        }
		
		delete p;
		up->msgh.length = strlen(up->content) + 1;
		write(pfd2[1], &up, 4);
	}
}

int threadPoolInit()
{
    for (int i = 0; i < MaxThread; i++)
        pthread_create(stid + i, 0, threadFunc, 0);
    
    return 0;
}
